package com.quickcancellation.crm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quickcancellation.crm.config.LogAspect;
import com.quickcancellation.crm.pojo.dto.PageDto;
import com.quickcancellation.crm.pojo.dto.PageParamsDto;
import com.quickcancellation.crm.pojo.dto.ResponseDto;
import com.quickcancellation.crm.pojo.entity.Salearea;
import com.quickcancellation.crm.pojo.entity.Store;
import com.quickcancellation.crm.pojo.entity.Userinfo;
import com.quickcancellation.crm.pojo.vo.DicKeyValVo;
import com.quickcancellation.crm.pojo.vo.PrincipalVo;
import com.quickcancellation.crm.service.IDictionaryService;
import com.quickcancellation.crm.service.ISaleareaService;
import com.quickcancellation.crm.service.IStoreService;
import com.quickcancellation.crm.service.IUserinfoService;
import com.quickcancellation.crm.utils.DateUtils;
import com.quickcancellation.crm.utils.PageUtils;
import com.quickcancellation.crm.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-02-28
 */
@RestController
@RequestMapping("/store")
@CrossOrigin
@Api(tags = "终端/门店管理")
@SaCheckLogin
@SaCheckRole("store")
public class StoreController {

    @Resource
    private IStoreService storeService;

    @Resource
    private IUserinfoService userinfoService;

    @Resource
    private IDictionaryService dictionaryService;

    @Resource
    private ISaleareaService saleareaService;

    @Resource
    HttpServletRequest request;

    @PostMapping("/list")
    @ApiOperation("分页查询列表")
    @LogAspect
    public ResponseDto getStoreList(@RequestBody PageParamsDto<Map> params) {

        PageDto<Map> pageDto = PageUtils.getPageParams(params);

        List<Map> storeList = storeService.getStoreList(pageDto);

        return ResponseUtil.success(storeList.size(), storeList);
    }

    @PostMapping("/save")
    @ApiOperation("保存终端")
    @LogAspect
    public ResponseDto saveStore(@RequestBody Store store) {
        if (store.getStoreName() == null) {
            return ResponseUtil.fail("终端名称为空");
        }

        if (store.getStoreCode() == null) {
            return ResponseUtil.fail("终端编码为空");
        }

        if (storeService.list(new QueryWrapper<Store>().eq("storeName", store.getStoreName())).size() > 0) {
            return ResponseUtil.fail("终端名称已存在");
        }

        if (storeService.list(new QueryWrapper<Store>().eq("storeCode", store.getStoreCode())).size() > 0) {
            return ResponseUtil.fail("终端编码已存在");
        }

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);
        String usernumber = userInfo.getUsernumber();

        store.setCreateOp(usernumber);
        store.setUpdateOp(usernumber);
        store.setUpdateTime(DateUtils.getNow());
        store.setCreateTime(DateUtils.getNow());

        return ResponseUtil.ok(storeService.save(store));

    }

    @PostMapping("/delete")
    @ApiOperation("删除终端")
    @LogAspect
    public ResponseDto saveStore(@RequestBody Map params) {
        String storeId = MapUtils.getString(params, "id");

        boolean result = storeService.removeById(storeId);

        return ResponseUtil.ok(result);

    }

    @PostMapping("/update")
    @ApiOperation("修改终端")
    @LogAspect
    public ResponseDto updateStore(@RequestBody Store store) {

        if (store.getId() == null){
            ResponseUtil.fail("[id]为空");
        }
        store.setStoreCode(null);
        store.setStoreName(null);

        store.setCreateTime(null);
        store.setCreateOp(null);

        Userinfo userInfo = userinfoService.getUserInfoByRequest(request);

        String usernumber = userInfo.getUsernumber();

        store.setUpdateOp(usernumber);
        store.setUpdateTime(DateUtils.getNow());

        boolean result = storeService.updateById(store);

        return ResponseUtil.ok(result);

    }

    @PostMapping("/getStoreType")
    @ApiOperation("获取终端类型")
    @LogAspect
    public ResponseDto getStoreTypeList() {

        List<DicKeyValVo> dicList = dictionaryService.getDicKeyAndValByModel("终端类型");

        return ResponseUtil.success(dicList.size(),dicList);

    }


    @PostMapping("/getDivideType")
    @ApiOperation("获取终端渠道")
    @LogAspect
    public ResponseDto getDivideTypeList() {

        List<DicKeyValVo> dicList = dictionaryService.getDicKeyAndValByModel("终端渠道");

        return ResponseUtil.success(dicList.size(),dicList);

    }

    @PostMapping("/getprincipalList")
    @ApiOperation("获取客户负责人")
    @LogAspect
    public ResponseDto getStorePrincipalList() {

        List<PrincipalVo> principalVoList = userinfoService.getPrincipalListByRole("终端负责人");

        return ResponseUtil.success(principalVoList.size(),principalVoList);

    }


    @PostMapping("/getSaleAreaList")
    @ApiOperation("获取营销区域列表")
    @LogAspect
    public ResponseDto getSaleAreaList() {

//        List<PrincipalVo> principalVoList = userinfoService.getStorePrincipalListByRole("终端负责人");

        QueryWrapper<Salearea> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","saleName");
        List<Salearea> saleAreaList = saleareaService.list(queryWrapper);
        return ResponseUtil.success(saleAreaList.size(),saleAreaList);

    }



}
